package edu.nf.service.impl;


import edu.nf.SQLExecutor;
import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.nf.dao.AddEmpDao;
import edu.nf.dto.EmpAndDepartmDto;
import edu.nf.entity.Emp;
import edu.nf.exception.UpdateException;
import edu.nf.service.AddEmpService;
import edu.nf.utils.SQLExecutorFactory;

@Component("addEmpService")
public class AddEmpServiceImpl implements AddEmpService {


    @Inject(beanName = "addEmp")
    private AddEmpDao addEmpDao;

    @Override
    public void addEmp(EmpAndDepartmDto dto) {
        Emp emp = new Emp();
        emp.setDepartmId(dto.getDepartmId());
        emp.setEmpid(dto.getEmpId());
        emp.setEmpname(dto.getEmpname());
        emp.setSex(dto.getSex());
        emp.setPay(dto.getPay());

        SQLExecutor se = SQLExecutorFactory.getSQLExecutor();
        se.beginTransaction();
        try{

            addEmpDao.addEmp(emp);
            se.commit();
        }catch (RuntimeException e){
            se.rollback();
            throw new UpdateException("add fail.");
        }finally {
            SQLExecutorFactory.close();
        }

    }
}
